home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / SetCell / SetCell.c next >
Encoding:
C/C++ Source or Header  |  1994-03-31  |  8.5 KB  |  249 lines  |  [TEXT/MPS ]

  1. /*
  2. SetCell.c
  3. by Dale M. Greer, 31 March 1994
  4. Written with MPW C 3.3
  5. Internet: greer@utdallas.edu
  6.  
  7. ** Launches MicroSoft Excel and enters specified data
  8. ** into specified cell using AppleEvents
  9.  
  10. Many thanks to John Nagle who sent me an example
  11. of a received AppleEvent from a similar project.
  12.  
  13. The command we want to send is of the English form
  14. "set cell 2 of row 3 of the document named Worksheet1 to 2.3"
  15. The preposition "of" signals the existence of a container.  Objects in the 
  16. AppleEvent record must be nested as the containers are nested.  An English 
  17. version of the actual AppleEvent description of this English command might 
  18. look something like 
  19.  
  20. set data
  21.     (of object [cell
  22.         (of object [row
  23.             (of object [document named Worksheet1])
  24.         3]
  25.     2])
  26. to 2.3
  27.  
  28. In practice, the object part comes out like this...
  29.  
  30. wanttypeccelfromobj Ã¥wanttypecrowfromobj JwanttypedocufromnullformenumnameTEXT
  31. Worksheet1formenumindxseldTEXT3formenumindxseldTEXT2
  32.  
  33. ...but to make it you have to work top down, even though it comes 
  34. out backwards, Comprende?  First you make a Document container, 
  35. then put a row container inside that, then put a cell container 
  36. inside that.
  37.  
  38. The rest is fairly straightforward and is explained in the code.
  39.  
  40. DMG
  41.  
  42. */
  43.  
  44. #pragma mbg on
  45. #pragma processor 68020
  46. #pragma once
  47.  
  48. // #pragma load "SetCell.dumpfile" // Save time loading precompiled headers
  49.  
  50. #include <AEObjects.h>
  51. #include <AEPackObject.h>
  52. #include <AERegistry.h>
  53. #include <AppleEvents.h>
  54. #include <Controls.h>
  55. #include <Desk.h>
  56. #include <Dialogs.h>
  57. #include <Errors.h>
  58. #include <FCntl.h>
  59. #include <Fonts.h>
  60. #include <GestaltEqu.h>
  61. #include <Memory.h>
  62. #include <OSEvents.h>
  63. #include <OSUtils.h>
  64. #include <Quickdraw.h>
  65. #include <Resources.h>
  66. #include <stdio.h>
  67. #include <StdLib.h>
  68. #include <String.h>
  69. #include <TextEdit.h>
  70. #include <Types.h>
  71. #include <ToolUtils.h>
  72. #include <Windows.h>
  73.  
  74. void main (void);
  75. void SetCell(AEAddressDesc *theAddress, char *cell, char *row, char *data);
  76. void MyCreateDocContainer(AEDesc *myDocContainer, char *docName);
  77. void MyCreateCellContainer(char *cell, char *row, AEDesc *myCellContainer, char *docName);
  78. void MyCreateRowContainer(char *row, AEDesc *myRowContainer, char *docName);
  79. OSErr FindAndLaunchExcel(AEAddressDesc *theAddress);
  80.  
  81. #define kSearch 129        // Simple message DLOG used while waiting to find Excel
  82. LaunchParamBlockRec launchThis;
  83.  
  84. #define kAEName 'name'        // My AERegistry.h didn't have these, but I found them with the code to 
  85. #define kAEIndex 'indx'        // WritesWell Jr.  You can always just use the straight constants anyway.
  86.  
  87. void main (void)
  88. {
  89.     AEAddressDesc theAddress;
  90.     SetApplLimit((GetApplLimit())+8192);
  91.     
  92.     MaxApplZone();
  93.  
  94.     InitGraf((Ptr)&qd.thePort);
  95.     InitFonts();
  96.     InitWindows();
  97.     TEInit();
  98.     InitDialogs(nil);
  99.     InitCursor();
  100.     
  101.     FindAndLaunchExcel(&theAddress);
  102.     
  103.     SetCell(&theAddress, "1", "1", "2.3");
  104.     SetCell(&theAddress, "1", "2", "4.5");
  105.     SetCell(&theAddress, "1", "3", "=r1c1*r2c1");
  106. }
  107.  
  108. void SetCell(AEAddressDesc *theAddress, char *cell, char *row, char *data)
  109. {
  110.     OSErr err;
  111.     AppleEvent appleEvent, reply;
  112.     AEDesc myCellContainer, theData;
  113.  
  114.     err = AECreateAppleEvent(kAECoreSuite, kAESetData, theAddress,         // Start with a Set Data appleEvent
  115.             kAutoGenerateReturnID, 1L, &appleEvent);
  116.  
  117.     MyCreateCellContainer(cell, row, &myCellContainer, "Worksheet1");    // Append the destination descriptor
  118.     AEPutParamDesc(&appleEvent, keyDirectObject, &myCellContainer);        // to the appleEvent
  119.  
  120.     AECreateDesc(typeChar, data, strlen(data), &theData);                // Append the data descriptor
  121.     AEPutParamDesc(&appleEvent, keyAEData, &theData);                    // to the appleEvent
  122.  
  123.     err = AESend(&appleEvent, &reply, kAENoReply + kAENeverInteract,    // Send it to Excel
  124.             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  125.  
  126.     AEDisposeDesc(&myCellContainer);
  127.     AEDisposeDesc(&theData);
  128.     AEDisposeDesc(&appleEvent);
  129.     AEDisposeDesc(&reply);
  130. }
  131.  
  132. void MyCreateDocContainer(AEDesc *myDocContainer, char *docName)
  133. {
  134.     AEDesc myDocDescRec, nullDescRec;
  135.     OSErr err;
  136.  
  137.     err = AECreateDesc(typeNull, nil, 0, &nullDescRec);    // Prepare to make something from nothing 
  138.     err = AECreateDesc(typeChar, docName, strlen(docName), 
  139.             &myDocDescRec);                // This document container just contains the document name
  140.     err = CreateObjSpecifier(cDocument, &nullDescRec, kAEName,
  141.             &myDocDescRec, true, myDocContainer);        // Encapsulate it
  142.     AEDisposeDesc(&nullDescRec);
  143. }
  144.  
  145. void MyCreateRowContainer(char *row, AEDesc *myRowContainer, char *docName)
  146. {
  147.     AEDesc myDocDescRec, myRowDescRec;
  148.     OSErr err;
  149.  
  150.     MyCreateDocContainer(&myDocDescRec, docName);        // First you have to make the doc container 
  151.                                                         // into which the row container fits.
  152.     err = AECreateDesc(typeChar, row, strlen(row),
  153.             &myRowDescRec);                // This row container just contains the row number
  154.     err = CreateObjSpecifier(cRow, &myDocDescRec, kAEIndex,
  155.             &myRowDescRec, true, myRowContainer);        // Encapsulate it
  156.     AEDisposeDesc(&myDocDescRec);
  157. }
  158.  
  159. void MyCreateCellContainer(char *cell, char *row, AEDesc *myCellContainer, char *docName)
  160. {
  161.     AEDesc myCellDescRec, myRowDescRec;
  162.     OSErr err;
  163.  
  164.     MyCreateRowContainer(row, &myRowDescRec, docName);    // First you have to make the row container 
  165.                                                         // into which the cell container fits.
  166.     err = AECreateDesc(typeChar, cell, strlen(cell),
  167.             &myCellDescRec);            // This cell container just contains the cell number
  168.     err = CreateObjSpecifier(cCell, &myRowDescRec, kAEIndex,
  169.             &myCellDescRec, true, myCellContainer);        // Encapsulate it
  170.  
  171.     AEDisposeDesc(&myCellDescRec);
  172.     AEDisposeDesc(&myRowDescRec);
  173. }
  174.  
  175. // This function is from AE Interaction Sample by C.K.Haun of Apple DTS
  176. OSErr FindAndLaunchExcel(AEAddressDesc *theAddress)
  177. {
  178.     OSErr myError;
  179.     DialogPtr search = GetNewDialog(kSearch, nil, (WindowPtr)-1);
  180.     CSParamPtr csBlockPtr = NewPtrClear(sizeof(CSParam));
  181.     long dirIDUnused;
  182.     Str32 nulString = "\p";
  183.  
  184.     /* initialize the parameter block */
  185.     DrawDialog(search);
  186.  
  187.     if (csBlockPtr) {
  188.         csBlockPtr->ioSearchInfo1 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
  189.         csBlockPtr->ioSearchInfo2 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
  190.         if (csBlockPtr->ioSearchInfo1 && csBlockPtr->ioSearchInfo2) {
  191.             csBlockPtr->ioMatchPtr = (FSSpecPtr)NewPtrClear(sizeof(FSSpec) * 1);    /* only looking for 1 */
  192.             if (csBlockPtr->ioMatchPtr) {
  193.                 /* Now see if we can create an optimization buffer */
  194.                 csBlockPtr->ioOptBuffer = NewPtr(2048);
  195.                 if (csBlockPtr->ioOptBuffer)
  196.                     csBlockPtr->ioOptBufSize = 2048;
  197.                 else
  198.                     csBlockPtr->ioOptBufSize = 0;           /* no buffer, sorry */
  199.                 csBlockPtr->ioReqMatchCount = 1;
  200.                 csBlockPtr->ioSearchTime = 0;               /* no timeout */
  201.             }
  202.         }
  203.     }
  204.     HGetVol(nil, &csBlockPtr->ioVRefNum, &dirIDUnused);     /* get default volume for search */
  205.     csBlockPtr->ioSearchInfo1->hFileInfo.ioNamePtr = nil;
  206.     csBlockPtr->ioSearchInfo2->hFileInfo.ioNamePtr = nil;
  207.     csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdCreator = 'XCEL';    // Hardwired to launch Excel now
  208.     csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdType = 'APPL';
  209.     csBlockPtr->ioSearchBits = fsSBFlFndrInfo;
  210.     csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdCreator = 0xFFFFFFFF;
  211.     csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdType = 0xFFFFFFFF;
  212.     
  213.     myError = PBCatSearch(csBlockPtr, false);               /* search sync */
  214.     if (myError == noErr && csBlockPtr->ioActMatchCount != 0) {
  215.         /* we found it, so launch it */
  216.         
  217.         launchThis.launchBlockID = extendedBlock;
  218.         launchThis.launchEPBLength = extendedBlockLen;
  219.         launchThis.launchFileFlags = nil;
  220.         launchThis.launchControlFlags = launchContinue + launchNoFileFlags + launchDontSwitch;
  221.         launchThis.launchAppSpec = &csBlockPtr->ioMatchPtr[0];
  222.         myError = LaunchApplication(&launchThis);
  223.         if (myError == noErr) {
  224.             /* it launched fine.  we can use the PSN to make a target */
  225.             AECreateDesc(typeProcessSerialNumber, (Ptr)&launchThis.launchProcessSN, 
  226.                     sizeof(ProcessSerialNumber), theAddress);
  227.         }
  228.     }
  229.     
  230.     /* no matter what happened, kill the memory we had allocated */
  231.     if (csBlockPtr) {
  232.         if (csBlockPtr->ioSearchInfo1)
  233.             DisposPtr((Ptr)csBlockPtr->ioSearchInfo1);
  234.         if (csBlockPtr->ioSearchInfo2)
  235.             DisposPtr((Ptr)csBlockPtr->ioSearchInfo2);
  236.         if (csBlockPtr->ioMatchPtr)
  237.             DisposPtr((Ptr)csBlockPtr->ioMatchPtr);
  238.         if (csBlockPtr->ioOptBuffer)
  239.             DisposPtr((Ptr)csBlockPtr->ioOptBuffer);
  240.         DisposPtr((Ptr)csBlockPtr);
  241.     }
  242.     
  243.     /* catsearch section end */
  244.     DisposDialog(search);
  245.     return(myError);
  246. }
  247.  
  248.  
  249.